L.TasksManager = L.Class.extend({
	includes : L.Mixin.Events,

	initialize : function(couplingOptions) {

		this._tasks = {};
		this._couplingOptions = couplingOptions;

		if (this._couplingOptions && this._couplingOptions.enabled)
			this._couple(this._couplingOptions);
		// alert("couple");

	},
	addTask : function(id, options) {
		var task = new L.Task(id, options.options, options.type,
				options.instructions);
		this._tasks[id] = task;
		task.parent = options.parent;
		task.group = options.group;
		if (task.parent) {
			this._tasks[task.parent].instances.push(task.id);
		}
		$("li.minview[data-id='"+id+"']").addClass("task-minview");
		console.log(this._tasks[id]);
	},
	getRoomTask : function(id) {
		return this._tasks[id];
	},
	showResults : function(id) {
		var task = this.getRoomTask(id);
		console.log(task);
	},
	publish : function(id) {
		var groups = Skt.groupsManager.getWorkGroups();
		var context = this;
		$.each(groups, function(index, group) {
			var roomId = uniqueId();
			group.getRoomList().addRoom(roomId, Skt.user.id);
			Skt.model.copyRoom(id);
			Skt.model.pasteRoom(roomId);
			context._createInstance(id, roomId, group.getName());
		});
	},
	getSummary : function(id) {
		var task = this.getRoomTask(id);
		var context=this;
		if(task.group){
			var cnt=0;
			var cnt2=0;
			$.each(task.result,function(index,value){
				if(value.ok)
					cnt=cnt+1;
				cnt2=cnt2+1;
			});
			return {total:cnt2,ok:cnt};
		}else{
			var result={};
			$.each(task.instances,function(index,value){
				var subtask = context.getRoomTask(value);
				result[subtask.group]=context.getSummary(value);
			});
			console.log(result);
			return result;
		}
	},
	answer : function(id, userId, user, options) {
		var task = this.getRoomTask(id);
		var correctAnswer = [];
		var origin = task.parent || task.id;
		$.each(this.getRoomTask(origin).options, function(index, option) {
			if (option.answer) {
				correctAnswer.push(option.number);
			}
		});
		var ok = correctAnswer.length == options.length;
		if (ok) {
			$.each(correctAnswer, function(id, number) {
				if ($.inArray(number, options) < 0)
					ok = false;
				return ok;

			});
		}

		task.result[userId] = {
			userId : userId,
			user : user,
			ok : ok,
			answer : options
		};
		console.log(correctAnswer);
	},
	_createInstance : function(id, target, group) {
		var task = this._tasks[id];
		var nop = [];
		$.each(task.options, function(index, option) {
			nop.push({
				number : option.number,
				answer : false,
				value : option.value
			});
		});
		this.addTask(target, {
			options : nop,
			parent : task.id,
			type : task.type,
			instructions : task.instructions,
			group : group
		});
	},
	_couple : function(couplingOptions) {
		couplingOptions.adapter.coupleObject(couplingOptions.carriedId, this, {
			messageType : "EVENT",
			explicitMapping : [ "addTask", "answer" ]
		});
	}
});